package com.ecoolex.ying.space.core.manager;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.ying.space.core.dao.UserWorksMarktingDetailDAO;
import com.ecoolex.ying.space.entity.UserWorksMarktingDetail;
import com.ecoolex.ying.space.entity.UserWorksMarktingDetail.Column;
import com.ecoolex.ying.space.entity.UserWorksMarktingDetailExample;
import com.ecoolex.ying.space.entity.UserWorksMarktingDetailExample.Criteria;
import com.ecoolex.ying.space.pojo.AddUserWorksMarktingDetailDTO;

import java.time.Instant;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserWorksMarktingDetailManager {
	
    @Autowired
    private UserWorksMarktingDetailDAO dao;
    
    public UserWorksMarktingDetail getDetail(Integer id) {
		return dao.selectByPrimaryKeyWithLogicalDelete(id, UserWorksMarktingDetail.NOT_DELETED);
	}
    
    public UserWorksMarktingDetail getByMarktingIdAndWorksId(Integer marktingId, Integer worksId) {
    	UserWorksMarktingDetailExample example = new UserWorksMarktingDetailExample();
		Criteria criteria = example.createCriteria();
		criteria.andMarktingIdEqualTo(marktingId).andWorksIdEqualTo(worksId).andDeletedEqualTo(UserWorksMarktingDetail.NOT_DELETED);
    	return dao.selectOneByExample(example);
	}

	public Pagination<UserWorksMarktingDetail> queryDetailByMarktingId(Integer marktingId, Page page) {
		UserWorksMarktingDetailExample example = new UserWorksMarktingDetailExample();
		Criteria criteria = example.createCriteria();
		criteria.andMarktingIdEqualTo(marktingId).andDeletedEqualTo(UserWorksMarktingDetail.NOT_DELETED);
		example.setOffset(page.getFirstIndex());
		example.setRows(page.getPageSize());
		example.orderBy(Column.weight.desc());
		
		return Pagination.create(page, 
			dao.countByExample(example),
			() -> dao.selectByExample(example));
	}

	private void verify(Integer marktingId,UserWorksMarktingDetail detail) {
		BizAssert.isTrue(Check.notNull(detail.getWorksId()),ResultCode.VERIFY.build(10), "作品Id为空");
		UserWorksMarktingDetail old = getByMarktingIdAndWorksId(marktingId,detail.getWorksId());
		BizAssert.isTrue(Check.isNull(old),ResultCode.VERIFY.build(10), "此营销位，ID={},已经存在这个作品,ID={}",marktingId,detail.getWorksId());
		BizAssert.isTrue(Check.notNull(detail.getWeight()),ResultCode.VERIFY.build(11), "权重为空");
	}
	
	public void addDetail(Integer marktingId, List<AddUserWorksMarktingDetailDTO> detailList, String createUser){
		for (UserWorksMarktingDetail detail : detailList) {
			verify(marktingId,detail);
			detail.setMarktingId(marktingId);
			detail.setDeleted(UserWorksMarktingDetail.NOT_DELETED);
			detail.setCreateUser(createUser);
			detail.setCreateTime(Date.from(Instant.now()));
			detail.setUpdateUser(createUser);
			detail.setUpdateTime(Date.from(Instant.now()));
			dao.insert(detail);
		}
	}

	public void editDetail(UserWorksMarktingDetail detail, String updateUser){
		detail.setUpdateUser(updateUser);
		detail.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(detail, Column.weight,Column.updateUser,Column.updateTime);
	}

	public void delDetail(UserWorksMarktingDetail detail, String updateUser){
		detail.setDeleted(UserWorksMarktingDetail.IS_DELETED);
		detail.setUpdateUser(updateUser);
		detail.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(detail, Column.deleted,Column.updateUser,Column.updateTime);
	}
}